package com.andatsoft.laisim.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import com.andatsoft.laisim.theme.ThemeManager;

public class LaisimBaseAnimation extends View {

	// private int layout_width = 100;// the width of the view
	// private int layout_height = 100;// the height of the view

	private int mColor = 0xff9988ff;
	private Paint mBackgroundPainter;
	private Paint mAnimationPainter;
	private PointF mTouchPointF = new PointF();
	private float mRadiusTouchAnimation = 75f;
	// calculate the exactly the postion of the touch view
	private Rect mRect = new Rect();

	private boolean mShouldRemoveCallBack;
	private int mAlpha = 50;

	/**
	 * This will change from 0 to mRadiusTouchAnimation
	 */
	private float mRadiusTouchAnimationProgress = 0f;

	public LaisimBaseAnimation(Context context) {
		super(context);
		// this.mContext = context;
	}

	public LaisimBaseAnimation(Context context, AttributeSet attrs) {
		super(context, attrs);
		// this.mContext = context;
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		getGlobalVisibleRect(mRect);
		setupPainter();
	}

	private void setupPainter() {
		if (mBackgroundPainter == null)
			mBackgroundPainter = new Paint();
		mBackgroundPainter.setAntiAlias(true);
		mBackgroundPainter.setColor(mColor);
		mBackgroundPainter.setStyle(Paint.Style.FILL);
		mBackgroundPainter.setAlpha(mAlpha);

		if (mAnimationPainter == null)
			mAnimationPainter = new Paint();
		mAnimationPainter.setAntiAlias(true);
		mAnimationPainter.setColor(mColor);
		mBackgroundPainter.setStyle(Paint.Style.FILL);
		mAnimationPainter.setAlpha(50);
	}

	public void applyTheme() {
		if (ThemeManager.getSelectedTheme() != null) {
			mColor = ThemeManager.getSelectedTheme().getNormalTextColor();
		}
		if (mBackgroundPainter != null) {
			mBackgroundPainter.setColor(mColor);
			mBackgroundPainter.setAlpha(mAlpha);
		}
		if (mAnimationPainter != null) {
			mAnimationPainter.setColor(mColor);
			mAnimationPainter.setAlpha(50);
		}
	}

	private Runnable mUpdateAnimation = new Runnable() {

		@Override
		public void run() {
			if (mRadiusTouchAnimationProgress > mRadiusTouchAnimation - 25) {
				mRadiusTouchAnimationProgress += 3;
				mAlpha -= 3;
			} else {
				mRadiusTouchAnimationProgress += 5;
				mAlpha -= 5;
			}
			if (mAlpha < 0)
				mAlpha = 0;

			mBackgroundPainter.setAlpha(mAlpha);
			invalidate();
			if (mRadiusTouchAnimationProgress > mRadiusTouchAnimation) {
				mRadiusTouchAnimationProgress = 0f;
				mBackgroundPainter.setAlpha(0);
				mHandler.removeCallbacks(mUpdateAnimation);
				mShouldRemoveCallBack = true;
			}
		}
	};

	private Handler mHandler = new Handler();

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// draw background first
		canvas.drawCircle(mTouchPointF.x, mTouchPointF.y,
				mRadiusTouchAnimation, mBackgroundPainter);

		// draw animation
		canvas.drawCircle(mTouchPointF.x, mTouchPointF.y,
				mRadiusTouchAnimationProgress, mAnimationPainter);
		if (!mShouldRemoveCallBack)
			mHandler.post(mUpdateAnimation);

	}

	public void animate(float x, float y) {
		mShouldRemoveCallBack = false;
		mTouchPointF.x = x;
		mTouchPointF.y = y - mRect.top;
		mAlpha = 50;
		mRadiusTouchAnimationProgress = mRadiusTouchAnimation / 3;
		mBackgroundPainter.setAlpha(mAlpha);
		invalidate();
	}
	
//	public void animate(float x, float y) {
//		mShouldRemoveCallBack = false;
//		mTouchPointF.x = x;
//		mTouchPointF.y = y - mRect.top;
//		mAlpha = 50;
//		mRadiusTouchAnimationProgress = mRadiusTouchAnimation / 3;
//		mBackgroundPainter.setAlpha(mAlpha);
//		invalidate();
//	}

}
